﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DataStorage {

    private const int TILE_WIDTH = 1;
    private const int TILE_HEIGHT = 1;

    private const int MIN_ROOMS = 3;
    private const int MAX_ROOMS = 6;

    private const int MAP_WITDH = 22; //map width in tiles
    private const int MAP_HEIGHT = 22; // map height " "

    private const int MIN_ROOM_SIZE = 6; //max room size in tiles
    private const int MAX_ROOM_SIZE = 15; //min room size in tiles

    private const int COUNTDOWN_BEGINNING = 20;

    private const int WALL_THICKNESS = 1;
    private const float WALL_HEIGHT = 1;

    private List<Room> rooms = new List<Room>();

    private List<GameObject> furniture = new List<GameObject>();
    private List<GameObject> floors = new List<GameObject>();

    private List<Edge> edges = new List<Edge>();
    private List<Edge> usedEdges = new List<Edge>();

    private static DataStorage _instance = null;
    public static DataStorage Instance
    {
        get
        {
            lock (typeof(DataStorage))
            {
                if (_instance == null)
                {
                    _instance = new DataStorage();
                }
            }
            return _instance;
        }
    }

    //RoomList 
    public List<Room> GetRoomList()
    {
        return this.rooms;
    }
    public void SetRoomList(List<Room> rooms)
    {
        this.rooms = rooms;
    }
    //EdgeList
    public List<Edge> GetEdgeList()
    {
        return this.edges;
    }
    public void SetEdgeList(List<Edge> edges)
    {
        this.edges = edges;
    }
    //UsedEdgeList
    public List<Edge> GetUsedEdgeList()
    {
        return this.usedEdges;
    }
    public void SetUsedEdgeList(List<Edge> edges)
    {
        this.usedEdges = edges;
    }
    //Tile
    public int GetTileWidth()
    {
        return TILE_WIDTH;
    }
    public int GetTileHeight()
    {
        return TILE_HEIGHT;
    }

    public int GetMinRooms()
    {
        return MIN_ROOMS;
    }
    public int GetMaxRooms()
    {
        return MAX_ROOMS;
    }

    public int GetMapWidth()
    {
        return MAP_WITDH;
    }
    public int GetMapHeight()
    {
        return MAP_HEIGHT;
    }

    public int GetMinRoomSize()
    {
        return MIN_ROOM_SIZE;
    }
    public int GetMaxRoomSize()
    {
        return MAX_ROOM_SIZE;
    }

    public int GetWallThickness()
    {
        return WALL_THICKNESS;
    }

    public float GetWallHeight()
    {
        return WALL_HEIGHT;
    }

    public int GetCountdownBeginning()
    {
        return COUNTDOWN_BEGINNING;
    }

    public List<GameObject> GetFurniturePrefabs()
    {
        return furniture;
    }
    public void SetFurniturePrefabs()
    {
        //Toilet
        GameObject toilet = Resources.Load("Prefabs/Furniture/Toilet", typeof(GameObject)) as GameObject;
        GameObject temp = (GameObject)GameObject.Instantiate(toilet);
        temp.name = "Toilet";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Toilet);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Bathroom);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(1, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(7.17f, 7.831992f, 10.99f));

        
        furniture.Add(temp);

        //Bathtub
        GameObject bathtub = Resources.Load("Prefabs/Furniture/Bathtub", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(bathtub);
        temp.name = "Bathtub";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Bathtub);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Bathroom);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(1, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(29.11f, 6.06736f, 21.2f));
        furniture.Add(temp);

        //Laundromat
        GameObject laundromat = Resources.Load("Prefabs/Furniture/Laundromat", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(laundromat);
        temp.name = "Laundromat";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Laundromat);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Bathroom);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(0, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(1.65f, 1.149294f, 2.1f));
        furniture.Add(temp);

        //LaundryBasket
        GameObject laundryBasket = Resources.Load("Prefabs/Furniture/LaundryBasket", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(laundryBasket);
        temp.name = "LaundryBasket";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.LaundryBasket);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Bathroom);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(5, 4, FurnitureType.Floor);
        furniture.Add(temp);

        //Sink
        GameObject sink = Resources.Load("Prefabs/Furniture/Sink", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(sink);
        temp.name = "Sink";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Sink);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Bathroom);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(1, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(5.96f, 5.34f, 10.77f));
        furniture.Add(temp);

        //Table
        GameObject table = Resources.Load("Prefabs/Furniture/Table", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(table);
        temp.name = "Table";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Table);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.LivingRoom);
        temp.GetComponent<FurniturePiece>().SetMinInstances(1);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(2);
        //temp.GetComponent<FurniturePiece>().SetFace(0, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetFace(5, 4, FurnitureType.Floor);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(1.27678f, 0.6985102f, 1f));
        furniture.Add(temp);
        
        //Chair
        GameObject chair = Resources.Load("Prefabs/Furniture/Chair", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(chair);
        temp.name = "Chair";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Chair);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.LivingRoom);
        temp.GetComponent<FurniturePiece>().SetMinInstances(4);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(10);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesAsChild(10);
        temp.GetComponent<FurniturePiece>().SetFace(1, 0, FurnitureType.Table);
        temp.GetComponent<FurniturePiece>().SetFace(1, 1, FurnitureType.Table);
        temp.GetComponent<FurniturePiece>().SetFace(1, 2, FurnitureType.Table);
        temp.GetComponent<FurniturePiece>().SetFace(1, 3, FurnitureType.Table);
        furniture.Add(temp);

        //Couch
        GameObject couch = Resources.Load("Prefabs/Furniture/Couch", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(couch);
        temp.name = "Couch";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Couch);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.LivingRoom);
        temp.GetComponent<FurniturePiece>().SetMinInstances(1);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(2);
        temp.GetComponent<FurniturePiece>().SetFace(0, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(1.87f, 1.246161f, 3.4f));
        furniture.Add(temp);

        //CouchTable
        GameObject couchTable = Resources.Load("Prefabs/Furniture/CouchTable", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(couchTable);
        temp.name = "CouchTable";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Table);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.LivingRoom);
        temp.GetComponent<FurniturePiece>().SetMinInstances(1);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(0, 0, FurnitureType.Wall); //TODO ändern in TV
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(61.7f, 45.5f, 41.1701f));
        furniture.Add(temp);

        //Bed
        GameObject bed = Resources.Load("Prefabs/Furniture/Bed", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(bed);
        temp.name = "Bed";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Bed);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.BedRoom);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(0, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(1.8f, 1.046682f, 3.09f));
        furniture.Add(temp);

        //Closet
        GameObject closet = Resources.Load("Prefabs/Furniture/Closet", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(closet);
        temp.name = "Closet";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Closet);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.BedRoom);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(3, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(1.63f, 2.177503f, 1.61f));
        furniture.Add(temp);

        //OfficeChair
        GameObject officeChair = Resources.Load("Prefabs/Furniture/OfficeChair", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(officeChair);
        temp.name = "OfficeChair";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Chair);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Office);
        temp.GetComponent<FurniturePiece>().SetMinInstances(1);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(2);
        temp.GetComponent<FurniturePiece>().SetFace(1, 0, FurnitureType.Table);
        //temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(1.18f, 1.513857f, 1.26f));
        furniture.Add(temp);

        //OfficeTable
        GameObject officeTable = Resources.Load("Prefabs/Furniture/OfficeTable", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(officeTable);
        temp.name = "OfficeTable";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Table);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Office);
        temp.GetComponent<FurniturePiece>().SetMinInstances(1);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(1, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(61.7f, 45.5f, 2.954591f));
        furniture.Add(temp);

        //Printer
        GameObject printer = Resources.Load("Prefabs/Furniture/Printer", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(printer);
        temp.name = "Printer";
        temp.AddComponent<FurniturePiece>();
        temp.GetComponent<FurniturePiece>().Clean();
        temp.GetComponent<FurniturePiece>().SetFurnitureType(FurnitureType.Printer);
        temp.GetComponent<FurniturePiece>().SetPossibleRooms(RoomType.Office);
        temp.GetComponent<FurniturePiece>().SetMinInstances(1);
        temp.GetComponent<FurniturePiece>().SetMaxInstancesInRoom(1);
        temp.GetComponent<FurniturePiece>().SetFace(0, 0, FurnitureType.Wall);
        temp.GetComponent<FurniturePiece>().SetOtherColliderValues(new Vector3(61.7f, 45.5f, 41.1701f));
        furniture.Add(temp);

        //FurniturePiece allo = furniture[2].GetComponent<FurniturePiece>();
    }

    public List<GameObject> GetFloorPrefabs()
    {
        return floors;
    }
    public void SetFloorPrefabs()
    {
        GameObject bathroomFloor = Resources.Load("Prefabs/Floors/FloorBathroom", typeof(GameObject)) as GameObject;
        GameObject temp = (GameObject)GameObject.Instantiate(bathroomFloor);
        temp.name = "FloorBathroom";
        floors.Add(temp);

        GameObject livingroomFloor = Resources.Load("Prefabs/Floors/FloorLivingroom", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(livingroomFloor);
        temp.name = "FloorLivingroom";
        floors.Add(temp);

        GameObject bedroomFloor = Resources.Load("Prefabs/Floors/FloorBedroom", typeof(GameObject)) as GameObject;
        temp = (GameObject)GameObject.Instantiate(bedroomFloor);
        temp.name = "FloorBedroom";
        floors.Add(temp);


    }
}
